﻿/// <reference path="liveSDK.js" />


var client_id = "%CLIENT_ID%",
        scope = ["wl.signin", "wl.basic", "wl.offline_access"],
        redirect_uri = "%REDIRECT_URI_PATH%/callback.aspx";

function id(domId) {
    return document.getElementById(domId);
}

function displayMe() {
    var imgHolder = id("meImg"),
            nameHolder = id("meName");

    if (imgHolder.innerHTML != "") return;

    if (WL.getSession() != null) {
        WL.api({ path: "me/picture", method: "get" }).then(
                    function (response) {
                        if (response.location) {
                            imgHolder.innerHTML = "<img src='" + response.location + "' />";
                        }
                    }
                );

        WL.api({ path: "me", method: "get" }).then(
                    function (response) {
                        nameHolder.innerHTML = response.name;
                    }
                );
    }
}

function clearMe() {
    id("meImg").innerHTML = "";
    id("meName").innerHTML = "";
}

WL.Event.subscribe("auth.sessionChange",
        function (e) {
            if (e.session) {
                displayMe();
            }
            else {
                clearMe();
            }
        }
    );

WL.init({ client_id: client_id, redirect_uri: redirect_uri, response_type: "code" });

WL.ui({ name: "signin", element: "signin" });
